package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.AlarmDao;
import cn.topevery.garden.entity.po.Alarm;
import cn.topevery.garden.entity.query.AlarmQuery;
import cn.topevery.garden.entity.vo.AlarmVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.entity.SqlUpdateMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import hw.topevery.framework.entity.Value;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 
 *
 * @author code-generate
 * @date 2022-01-07
 */
@Repository
public class AlarmDaoImpl extends BaseDaoImpl<Alarm> implements AlarmDao {
    @Override
    public <P extends DbCsp> List<Alarm> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<AlarmVO> listPage(AlarmQuery query) {
        String table = "t_alarm t join t_sys_dict tsd on t.c_obj_type=tsd.c_dict_code";

        String columns = "t.*,tsd.c_dict_name as c_obj_type_name";

        int pageIndex = query.getPageIndex() - 1;
        int pageSize = query.getPageSize();

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("t.c_id desc");
        sqlQueryMate
                .where(true, "t.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "t.c_state", ScriptConditionEnum.Eq, 0)
                .where(true, "tsd.c_ns_id", ScriptConditionEnum.Eq, "25fed9f7-0e16-11eb-b5ae-00155deb0e04")
                .where(query.getType()!=null, "t.c_type", ScriptConditionEnum.Eq, query.getType())
                .where(StringUtils.isNotBlank(query.getObjType()), "t.c_obj_type", ScriptConditionEnum.Eq, query.getObjType())
                .where(StringUtils.isNotBlank(query.getAlarmContent()), "t.c_alarm_content", ScriptConditionEnum.Like, query.getAlarmContent())
                .where(StringUtils.isNotBlank(query.getBeginDate()), "t.c_alarm_date", ScriptConditionEnum.GreaterThanEq, query.getBeginDate())
                .where(StringUtils.isNotBlank(query.getEndDate()), "t.c_alarm_date", ScriptConditionEnum.LessThanEq, query.getEndDate())
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, AlarmVO.class);
    }

    @Override
    public Alarm getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public Integer szcgEvtAlarmEntry() {
        String sql = "insert into t_alarm(c_alarm_code,c_type,c_alarm_content,c_obj_type,c_obj_id,c_obj_name,c_alarm_date,c_state,c_remark,c_db_create_id,c_db_create_date,c_db_status,c_lng,c_lat)\n" +
                "select CONCAT('数字城管预警第',@rank:=@rank + 1+(select count(1) from t_alarm where c_obj_type='Alarm02'),'号'),0,CONTENT,'Alarm02',EVENT_ID,EVENT_NUM,now(),0,ADDR,'53b39c3c-5d35-4393-aedf-b178a1da8a13',now(),0,LOCATION_X,LOCATION_Y From v_szcg_evt vse, (SELECT @rank:= 0) b where LINK_STATE not in (1,10,28,27) and not exists(\n" +
                "select*From t_alarm ta where ta.c_obj_id=vse.EVENT_ID\n" +
                ")";
        Value<Integer> result = new Value<>();
        run(sql, CommandType.Text, dbCommand -> {
            result.data = dbCommand.executeNonResultSet();
        });
        return result.data;
    }

    @Override
    public Integer ocaEvtAlarmEntry() {
        String sql = "insert into t_alarm(c_alarm_code,c_type,c_alarm_content,c_obj_type,c_obj_id,c_obj_name,c_alarm_date,c_state,c_remark,c_db_create_id,c_db_create_date,c_db_status,c_lng,c_lat)\n" +
                "select CONCAT('绿地占用预警第',@rank:=@rank + 1+(select count(1) from t_alarm where c_obj_type='Alarm03'),'号'),0,c_oca_reason,'Alarm03',c_oca_id,c_permit_code,now(),0,c_oca_address,'53b39c3c-5d35-4393-aedf-b178a1da8a13',now(),0,0,0 From t_oca_info source, (SELECT @rank:= 0) b where c_db_status=0 and not exists(\n" +
                "select*From t_alarm target where target.c_obj_id=source.c_oca_id\n" +
                ")";
        Value<Integer> result = new Value<>();
        run(sql, CommandType.Text, dbCommand -> {
            result.data = dbCommand.executeNonResultSet();
        });
        return result.data;
    }

    @Override
    public Integer disastersAlarmEntry() {
        return 0;
    }

    @Override
    public Integer szcgEvtAlarmEnd() {
        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate("t_alarm ta join v_szcg_evt vse on ta.c_obj_id=vse.EVENT_ID");
        sqlUpdateMate.setColumnValue(true, "c_state", 1);
        sqlUpdateMate.where(true, "ta.c_obj_type", ScriptConditionEnum.Eq, "Alarm02")
                .where(true, "ta.c_state", ScriptConditionEnum.Eq, 0)
                .where(true, "vse.LINK_STATE", ScriptConditionEnum.Eq, 5);
        Value<Integer> result = new Value<>();
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
            result.data = dbCommand.executeNonResultSet();
        });
        return result.data;
    }
}
